package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.pojo.Bed;
import com.zzyl.nursing.domain.pojo.Floor;
import com.zzyl.nursing.domain.pojo.Room;
import com.zzyl.nursing.domain.vo.FloorVo;
import com.zzyl.nursing.domain.vo.TreeVo;
import com.zzyl.nursing.mapper.BedMapper;
import com.zzyl.nursing.mapper.FloorMapper;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 楼层Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor> implements IFloorService {
    @Autowired
    private FloorMapper floorMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BedMapper bedMapper;

    /**
     * 查询楼层
     *
     * @param id 楼层主键
     * @return 楼层
     */
    @Override
    public Floor selectFloorById(Long id) {
        return getById(id);
    }

    /**
     * 新增楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int insertFloor(Floor floor) {
        return save(floor) ? 1 : 0;
    }

    /**
     * 修改楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int updateFloor(Floor floor) {
        return updateById(floor) ? 1 : 0;
    }

    /**
     * 批量删除楼层
     *
     * @param ids 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除楼层信息
     *
     * @param id 楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有楼层（负责老人）
     */
    @Override
    public List<Floor> selectAllByNur() {
        return floorMapper.selectAllByNur();
    }

    /**
     * 根据床位状态查询获取所有楼层数据
     *
     * @param status
     * @return
     */
    @Override
    public List<TreeVo> getRoomAndBedByBedStatus(Integer status) {
//        List<TreeVo> treeVoList = floorMapper.getRoomAndBedByBedStatus(status);
//        return treeVoList;
        //1.查询所有楼
        List<Floor> floorList = this.list();

        //2.查询所有房(根据楼号查房)
        //2-1.获取楼号set集合
        Set<Long> floorIdSet = floorList.stream().map(Floor::getId).collect(Collectors.toSet());
        //2-2.查询所有房
        List<Room> roomList = roomMapper.selectList(Wrappers.lambdaQuery(Room.class).in(Room::getFloorId, floorIdSet));

        //3.查询所有床
        //3-1.获取房号set集合
        Set<Long> roomIdSet = roomList.stream().map(Room::getId).collect(Collectors.toSet());
        //3-2.查询所有床
        List<Bed> bedList = bedMapper.selectList(Wrappers.lambdaQuery(Bed.class).in(Bed::getRoomId, roomIdSet));

        //4.楼找房,房找床
        //4-1.构建床的map集合
        Map<Long, List<TreeVo>> bedMap = bedList.stream()
                .collect(Collectors.groupingBy(Bed::getRoomId, Collectors.mapping(bed -> TreeVo.builder().value(String.valueOf(bed.getId())).label(bed.getBedNumber()).build(), Collectors.toList())));
        //4-2.构建房的map集合
        Map<Long, List<TreeVo>> roomMap = roomList.stream().collect(Collectors.groupingBy(Room::getFloorId, Collectors.mapping(room -> TreeVo.builder().value(String.valueOf(room.getId())).label(room.getCode()).children(bedMap.getOrDefault(room.getId(), Collections.emptyList())).build(), Collectors.toList())));

        //5.将房的List集合转换为Vo属性的集合进行响应
        return floorList.stream().map(floor -> TreeVo.builder().value(String.valueOf(floor.getId())).label(floor.getName()).children(roomMap.getOrDefault(floor.getId(), Collections.emptyList())).build()).collect(Collectors.toList());
    }

    /**
     * 查询所有楼层（智能设备）
     */
    @Override
    public List<FloorVo> getAllFloorsWithDevice() {
        return this.getBaseMapper().getAllFloorsWithDevice();

    }

}